package org.wheres2.core.contactlist;

import java.util.Iterator;
import java.util.Vector;

/**
 * 
 * Implementa l'interfaccia ContactListManager
 * 
 * @author
 *
 */

public class ContactList implements ContactListManager {

	private Vector<Contact> list;
	
	
	public ContactList(){
		this.list = new Vector<Contact>(0);
	}

	/**
	 * 
	 * @see org.wheres2.core.contactlist.ContactListManager#addContact(java.lang.String, java.lang.String, boolean)
	 * 
	 */
	@Override
	public void addContact(String contact, String name, boolean online) throws InvalidContactNameException{
		Iterator<Contact> it = this.list.iterator();
		Contact tempContact;
		
		while(it.hasNext()){
			tempContact = (Contact)it.next();
			if( tempContact.getUserName().equals(contact) ){
				return;
			}
		}
		
		Contact listEntry = new Contact(contact, name);
		listEntry.setPresence(online);
		this.list.addElement(listEntry);
	}

	/**
	 * 
	 * @see org.wheres2.core.contactlist.ContactListManager#removeContact(java.lang.String)
	 * 
	 */
	@Override
	public void removeContact(String contact) {
		Iterator<Contact> it = this.list.iterator();
		Contact tempContact;
		
		while(it.hasNext()){
			tempContact = (Contact)it.next();
			if( tempContact.getUserName().equals(contact) ){
				this.list.removeElement(tempContact);
				return;
			}
		}
	}

	/**
	 *
	 * @see org.wheres2.core.contactlist.ContactListManager#getContact(java.lang.String)
	 * 
	 */
	@Override
	public ContactManager getContact(String contact) {
		Iterator<Contact> it = this.list.iterator();
		Contact tempContact;
		
		while(it.hasNext()){
			tempContact = (Contact)it.next();
			if( tempContact.getUserName().equals(contact)){
				return tempContact;
			}
		}
		return null;
	}

	/**
	 * 
	 * @see org.wheres2.core.contactlist.ContactListManager#getContacts()
	 * 
	 */
	@Override
	public Iterator<Contact> getContacts(){
		return this.list.iterator();
	}
	
	/**
	 *
	 * @see org.wheres2.core.contactlist.ContactListManager#containsContact(java.lang.String)
	 * 
	 */
	@Override
	public boolean containsContact(String contact){
		Iterator<Contact> it = this.list.iterator();
		while(it.hasNext()){
			if( ((Contact)it.next()).getUserName().equals(contact) )
				return true;
		}
		return false;
	}

}
